package com.github.llyb120.performance.dataset

class All{
    private final static ThreadLocal<LinkedList> local = new ThreadLocal<LinkedList>(){
        @Override
        protected LinkedList initialValue() {
            return new LinkedList()
        }
    }

    static Dataset dataset(Closure closure){
        def _dataset = new Dataset()
        pushStack(_dataset)
        closure.call()
        popStack()
        return _dataset
    }

    static Field field(Closure closure){
        def _field = new Field()
        pushStack(_field)
        closure.call()
        popStack()
        return _field
    }

    static Converter converter(Closure closure){
        def _converter = new Converter()
        pushStack(_converter)
        closure.call()
        popStack()
        return _converter
    }

    static void field(String val){
        def item = pickStack()
        if(item instanceof Dataset){
            item.getField().add(val)
        }
    }

    static void en(String val){
        def item = pickStack()
        if(item instanceof Field){
            item.setEn(val)
        }
    }

    static void cn(String val){
        def item = pickStack()
        if(item instanceof Field){
            item.setCn(val)
        }
    }

    static void setter(Closure val){
        def item = pickStack()
        if(item instanceof Field){
            item.setSetter(val)
        }
        if(item instanceof Converter){
            item.setSetter(val)
        }
    }

    static void type(Class val){
        def item = pickStack()
        if(item instanceof Field){
            item.setType(val)
        }
    }

    static void name(String val){
        def item = pickStack()
        if(item instanceof Converter){
            item.setName(val)
        }
    }

    static void getter(Closure val){
        def item = pickStack()
        if(item instanceof Converter){
            item.setGetter(val)
        }
    }

    private final static Object pickStack(){
        return local.get().getLast()
    }
    private final static Object pushStack(obj){
        local.get().addLast(obj)
    }
    private final static Object popStack(){
        return local.get().removeLast()
    }
}